#include "mylineedit.h"

#include <QDebug>

MyLineEdit::MyLineEdit(QWidget *parent, int width, int height): QLineEdit(parent)
{
    // 设置大小及位置
    int editWidth = width - 4;
    int editHeight = 20;

    this->resize(editWidth, editHeight);
    this->move((width - editWidth) / 2, (height - editHeight) / 2);

    // 设置其他属性
    this->setReadOnly(true);
    this->setAlignment(Qt::AlignCenter);

    // 输入结束让他不能输入了
    connect(this, &QLineEdit::editingFinished, [=](){
        this->setReadOnly(true);
    });

    QPalette p = this->palette();
    p.setColor(QPalette::Base, Qt::white);
    this->setAutoFillBackground(true);
    this->setPalette(p);
}

void MyLineEdit::mouseDoubleClickEvent(QMouseEvent *event)
{
    this->setReadOnly(false);
}

void MyLineEdit::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "MyLineEdit：" << __FUNCTION__;
    QLineEdit::mousePressEvent(event);
    event->ignore();
}

void MyLineEdit::mouseMoveEvent(QMouseEvent *event)
{
    QLineEdit::mouseMoveEvent(event);
    event->ignore();
}

void MyLineEdit::mouseReleaseEvent(QMouseEvent *event)
{
    QLineEdit::mouseReleaseEvent(event);
    event->ignore();
}

bool MyLineEdit::event(QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress) {
        qDebug() << "MyLineEdit：" << __FUNCTION__ << event->isAccepted();
    }

    return QLineEdit::event(event);
}
